package sort;

import java.util.Arrays;
import java.util.Random;

import static sort.Sort.binarySearch;


public class Test {
    public static void inorderArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
        }
    }

    public static void notInorderArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array.length - i;
        }
    }

    public static void initArray(int[] array) {
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(1000);
//            System.out.print(array[i]+" ");
        }
    }

    //测试
    public static void testSelectSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        Sort.selectSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("选择排序：" + (endTime - startTime));
    }

    public static void testInsertSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        Sort.insertSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("插入排序：" + (endTime - startTime));
    }

    public static void testShellSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        Sort.shellSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("希尔排序：" + (endTime - startTime));
    }

    public static void testQuickSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array,  array.length);
        long startTime = System.currentTimeMillis();
        Sort.shellSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("快速排序：" + (endTime - startTime));
    }
    public static void testHeapSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array,  array.length);
        long startTime = System.currentTimeMillis();
        Sort.shellSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("堆排序[是否乱序不影响]：" + (endTime - startTime));
    }

    public static void main2(String[] args) {
//        int[] array = {2, 1, 4, 3, 5,9,8,6,5,7};
//        int[] array = {5, 9, 8, 6, 5, 7};
        int[] array = {10, 6,7,1,3,9,4};
//        Sort.selectSort2(array);
//        Sort.quickSortNor(array);
//        Sort.quickSort(array);
        Sort.mergeSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
    }
    public static void main1(String[] args) {
        int[] array = new int[100000];
        inorderArray(array);
//        notInorderArray(array);
//        initArray(array);
//        testSelectSort(array);
//        testInsertSort(array);
//        testShellSort(array);
//        testHeapSort(array);
//        testQuickSort(array);

    }

    public static void main(String[] args) {
        // 创建一个有序数组
        int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};

        // 测试二分查找
        int target = 11;
        int result = binarySearch(sortedArray,10, target);
        System.out.println(result);
        // 输出结果
        if (result != -1) {
            System.out.println("Element found at index: " + result);
        } else {
            System.out.println("Element not found in the array.");
        }

    }

}
